package com.yt.base.util.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.text.SimpleDateFormat;
import java.util.List;

public class JacksonMapper extends ObjectMapper {
	private static final long serialVersionUID = 0x111b3cde7dd526c1L;

	public JacksonMapper() {
	}

	public JacksonMapper(String dateFormat) {
		setDateFormat(new SimpleDateFormat(dateFormat));
	}

	public String toJson(Object object) {
		try {
			return writeValueAsString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		throw new RuntimeException("解析对象错误");
	}

	public List toList(String json) {
		try {
			return (List) readValue(json, List.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		throw new RuntimeException("解析json错误");
	}

	public Object toObject(String json, Class clazz) {
		try {
			return readValue(json, clazz);
		} catch (Exception e) {
			e.printStackTrace();
		}
		throw new RuntimeException("解析json错误");
	}

	public String toPageJson(List list, Integer totalCounts) {
		StringBuffer sb = (new StringBuffer("{success:true,totalCounts:")).append(totalCounts).append(",result:");
		sb.append(toJson(list));
		sb.append("}");
		return sb.toString();
	}

	public String toDataJson(Object object) {
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(toJson(object));
		sb.append("}");
		return sb.toString();
	}

	public String toResultJson(Object object) {
		StringBuffer sb = new StringBuffer("{success:true,result:");
		sb.append(toJson(object));
		sb.append("}");
		return sb.toString();
	}

}
